<!--  线 + 柱混合图 -->
<template>
  <el-card>
    <template #header>
      <div class="title">
        近一周内空气质量反馈情况
        <el-tooltip effect="dark" content="点击试试下载" placement="bottom">
          <i-ep-download class="download" @click="downloadEchart" />
        </el-tooltip>
      </div>
    </template>

    <div :id="id" :class="className" :style="{ height, width }"></div>
  </el-card>
</template>

<script setup lang="ts">
import * as echarts from "echarts";
import DashboardAPI from "@/api/dashboard";

const props = defineProps({
  id: {
    type: String,
    default: "barChart",
  },
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "200px",
    required: true,
  },
  height: {
    type: String,
    default: "200px",
    required: true,
  },
});

const option = {
  color: ["#80FFA5", "#00DDFF", "#37A2FF", "#FF0087", "#FFBF00"],

  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "cross",
      label: {
        backgroundColor: "#6a7985",
      },
    },
  },
  legend: {
    data: ["AQI超标数", "AQI合格数"],
  },
  toolbox: {},
  grid: {
    left: "3%",
    right: "4%",
    bottom: "3%",
    containLabel: true,
  },
  xAxis: [
    {
      type: "category",
      boundaryGap: false,
      data: [],
    },
  ],
  yAxis: [
    {
      type: "value",
    },
  ],
  series: [
    {
      name: "AQI超标数",
      type: "line",
      smooth: true,
      lineStyle: {
        width: 1,
      },
      showSymbol: false,
      areaStyle: {
        opacity: 0.8,
        color: "#5cd3d5",
      },
      emphasis: {
        focus: "series",
      },
      data: [],
    },
    {
      name: "AQI合格数",
      type: "line",
      smooth: true,
      lineStyle: {
        width: 1,
      },
      showSymbol: false,
      areaStyle: {
        opacity: 0.8,
        color: "#2588e4",
      },
      emphasis: {
        focus: "series",
      },
      data: [],
    },
  ],
};

const downloadEchart = () => {
  // 获取画布图表地址信息
  const img = new Image();
  img.src = chart.value.getDataURL({
    type: "png",
    pixelRatio: 1,
    backgroundColor: "#fff",
  });
  // 当图片加载完成后，生成 URL 并下载
  img.onload = () => {
    const canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    const ctx = canvas.getContext("2d");
    if (ctx) {
      ctx.drawImage(img, 0, 0, img.width, img.height);
      const link = document.createElement("a");
      link.download = `业绩柱状图.png`;
      link.href = canvas.toDataURL("image/png", 0.9);
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
  };
};

function reqData() {
  DashboardAPI.query1().then((res) => {
    option.series[0].data = [];
    option.series[1].data = [];
    option.xAxis[0].data = [];
    const r1 = [];
    const r2 = [];
    const r3 = [];
    for (const v of res) {
      r1.push(v.overNum);
      r2.push(v.fineNum);
      r3.push(formatDate(new Date(v.date)));
    }
    r1.reverse();
    r2.reverse();
    r3.reverse();
    option.series[0].data = r1;
    option.series[1].data = r2;
    option.xAxis[0].data = r3;
    chart.value.setOption(option);
  });
}

function pad(num: any) {
  return num.toString().padStart(2, "0");
}

function formatDate(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${pad(month)}-${pad(day)}`;
}

const chart = ref<any>("");
onMounted(() => {
  // 图表初始化
  chart.value = markRaw(
    echarts.init(document.getElementById(props.id) as HTMLDivElement)
  );

  // chart.value.setOption(option);
  reqData();
  setInterval(() => {
    reqData();
  }, 5000);

  // 大小自适应
  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});

onActivated(() => {
  if (chart.value) {
    chart.value.resize();
  }
});
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  justify-content: space-between;

  .download {
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}
</style>
